package com.interview.contacts.respository;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.interview.contacts.entity.Contacts;
import com.interview.contacts.entity.Contacts_;
import com.interview.contacts.query.ContactsConditionQuery;
import com.interview.core.page.Page;
import com.interview.customer.entity.Customer;
import com.interview.customer.query.CustomerConditionQuery;

@Repository
public class ContactsRespository {
	@PersistenceContext
	private EntityManager entityManager;

	public Contacts findOne(String id) {
		return entityManager.find(Contacts.class, id);
	}

	public void save(Contacts contacts) {
		entityManager.persist(contacts);
	}

	public Contacts update(Contacts contacts) {
		entityManager.merge(contacts);
		return contacts;
	}

	public void delete(String id) {
		entityManager.remove(findOne(id));
	}

	public List<Contacts> findAllContacts() {
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<Contacts> criteriaQuery = criteriaBuilder
				.createQuery(Contacts.class);
		Root<Contacts> root = criteriaQuery.from(Contacts.class);

		List<Contacts> contactses = entityManager.createQuery(criteriaQuery)
				.getResultList();

		return contactses;
	}

	public Page findContactsByContactsConditionQuery(

	ContactsConditionQuery conditionQuery) {

		String hqlHead = "select con from Customer  cus join cus.contacts  con  where cus.id = :id ";
		StringBuilder hql = new StringBuilder();

		String cus_id = conditionQuery.getCus_id();

		String duty = conditionQuery.getDuty();
		if(duty==""||duty.contains(" ")){
			duty=null;
		}
		if (duty != null) {
			duty = "%" + duty + "%";
		}
		
		String name = conditionQuery.getName();
		
		if(name==""||name.contains(" ")){
			name=null;
		}
		if (name != null) {
			
			name = "%" + name + "%";
		}
		
		Integer pageIndex = conditionQuery.getPageIndex();
		Integer pageSize = conditionQuery.getPageSize();

		hql.append(hqlHead);

		if (name != null) {

			String nameHql = "and con.name like :name ";
			hql.append(nameHql);

		}
		if (duty != null) {

			String dutyHql = "and con.duty like :duty ";
			hql.append(dutyHql);

		}

		Query query = entityManager.createQuery(hql.toString());

		query.setParameter("id", cus_id);

		if (name != null) {
			query.setParameter("name", name);
		}

		if (duty != null) {
			query.setParameter("duty", duty);
		}

		Integer total = query.getResultList().size();

		if (pageIndex < 0) {
			pageIndex = 0;
		}

		if (pageSize < 0) {
			pageSize = 10;
		}

		List<Contacts> contacts = query.setFirstResult(pageIndex * pageSize)
				.setMaxResults(pageSize).getResultList();

		Page page = new Page();
		page.setList(contacts);
		page.setTotal(total);
		page.setPageIndex(pageIndex);
		page.setPageSize(pageSize);

		return page;
	}

}
